package st1417;

import java.util.ArrayList;

class Solution {
    public String reformat(String s) {
        ArrayList<Character> chars = new ArrayList<>();
        ArrayList<Character> nums = new ArrayList<>();
        for(int i=0;i<s.length();i++){
            char temp =s.charAt(i);
            if (Character.isDigit(temp)){
                nums.add(temp);
            }else{
                chars.add(temp);
            }
        }
        int j=chars.size()-nums.size();
        StringBuilder sb = new StringBuilder(chars.size()+nums.size());
        if(j==1||j==0){
            for (int i = 0; i < nums.size(); i++) {
                sb.append(chars.get(i));
                sb.append(nums.get(i));
            }
            if(j==1){
                sb.append(chars.get(nums.size()));
            }
            return sb.toString();
        }else if(j==-1){
            for (int i = 0; i < chars.size(); i++) {
                sb.append(nums.get(i));
                sb.append(chars.get(i));
            }
            sb.append(nums.get(chars.size()));
            return sb.toString();
        }else {
            return "";
        }
    }
    public static void main(String[] args) {
        String a = "gggdsg1234567";
        Solution s= new Solution();
        System.out.println(s.reformat(a));
    }
}
